"""
本代码由[Tkinter布局助手]生成
官网:https://www.pytk.net
QQ交流群:905019785
在线反馈:https://support.qq.com/product/618914
"""
import random
from tkinter import *
from tkinter.ttk import *
from lib.osc import *

def btn_live(ipt,iptc):
    input_content = ipt.get()
    print(f"开始加载OSC文件: {input_content}")
    thread = threading.Thread(target=delay_set, args=[0])
    thread.start()

    thread = threading.Thread(target=advance_set, args=[0])
    thread.start()


    global livea
    livea = threading.Thread(target=live, args=[input_content])
    livea.start()

    global timelabel
    timelabel = iptc

    timer = threading.Timer(0.5, change_time)
    timer.start()

def change_time():
    time_int = time_get()//10

    hours, remainder = divmod(time_int, 3600)
    minutes, seconds = divmod(remainder, 60)
    time =  f"{hours:02d}:{minutes:02d}:{seconds:02d}"

    timelabel.config(text=str(time))

    timer = threading.Timer(0.5, change_time)
    timer.start()

def btn_stop():
    print(f"停止加载OSC文件")
    try:
        livea.join(0)  # 使用join()立即中断线程
    except:
        print(f"停止加载OSC文件 - Fail")

def btn_record(ipt,iptc):

    input_content = ipt.get()
    print(f"开始记录OSC文件")
    global record
    record = threading.Thread(target=record, args=[input_content])
    record.start()

    global timelabel
    timelabel = iptc

    timer = threading.Timer(0.5, change_time)
    timer.start()

def btn_stopsound():
    print(f"关闭声音")
    fun_sound_stop()

def btn_live_delay(ipt):
    try:
        input_content = ipt.get()
        if input_content=="":
            input_content = 0
    except:
        input_content = 0
    print(f"设置延迟值: {input_content}")
    thread = threading.Thread(target=delay_set, args=[input_content])
    thread.start()

def btn_playsound_stop():
    print(f"停止播放声音")
    thread = threading.Thread(target=fun_sound_stop, args=[])
    thread.start()

def btn_live_advance(ipt):
    try:
        input_content = ipt.get()
        if input_content=="":
            input_content = 0
    except:
        input_content = 0
    print(f"设置提前值: {input_content}")
    thread = threading.Thread(target=advance_set, args=[input_content])
    thread.start()


class WinGUI(Tk):
    def __init__(self):
        super().__init__()
        self.__win()
        self.tk_label_lwlmbyq6 = self.__tk_label_lwlmbyq6(self)
        self.tk_input_lwlmc3ut = self.__tk_input_lwlmc3ut(self)
        self.tk_button_lwlmc5xy = self.__tk_button_lwlmc5xy(self)
        self.tk_button_lwlml4xr = self.__tk_button_lwlml4xr(self)
        self.tk_button_lwlmlf6t = self.__tk_button_lwlmlf6t(self)
        self.tk_input_lwlmm4al = self.__tk_input_lwlmm4al(self)
        self.tk_input_lwlmnfdy = self.__tk_input_lwlmnfdy(self)
        self.tk_button_lwlmnili = self.__tk_button_lwlmnili(self)
        self.tk_input_lwlmq24f = self.__tk_input_lwlmq24f(self)
        self.tk_button_lwlmq4om = self.__tk_button_lwlmq4om(self)
        self.tk_label_lwlmqt4u = self.__tk_label_lwlmqt4u(self)
        self.tk_label_lwlmslf1 = self.__tk_label_lwlmslf1(self)
        self.tk_button_lwlp9o5j = self.__tk_button_lwlp9o5j(self)
        self.tk_button_lwlpelpp = self.__tk_button_lwlpelpp(self)
    def __win(self):
        self.title("OSC Control  - DJ_KAI_STUDIO")
        # 设置窗口大小、居中
        width = 597
        height = 334
        screenwidth = self.winfo_screenwidth()
        screenheight = self.winfo_screenheight()
        geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        self.geometry(geometry)

        self.resizable(width=False, height=False)

    def scrollbar_autohide(self,vbar, hbar, widget):
        """自动隐藏滚动条"""
        def show():
            if vbar: vbar.lift(widget)
            if hbar: hbar.lift(widget)
        def hide():
            if vbar: vbar.lower(widget)
            if hbar: hbar.lower(widget)
        hide()
        widget.bind("<Enter>", lambda e: show())
        if vbar: vbar.bind("<Enter>", lambda e: show())
        if vbar: vbar.bind("<Leave>", lambda e: hide())
        if hbar: hbar.bind("<Enter>", lambda e: show())
        if hbar: hbar.bind("<Leave>", lambda e: hide())
        widget.bind("<Leave>", lambda e: hide())

    def v_scrollbar(self,vbar, widget, x, y, w, h, pw, ph):
        widget.configure(yscrollcommand=vbar.set)
        vbar.config(command=widget.yview)
        vbar.place(relx=(w + x) / pw, rely=y / ph, relheight=h / ph, anchor='ne')
    def h_scrollbar(self,hbar, widget, x, y, w, h, pw, ph):
        widget.configure(xscrollcommand=hbar.set)
        hbar.config(command=widget.xview)
        hbar.place(relx=x / pw, rely=(y + h) / ph, relwidth=w / pw, anchor='sw')
    def create_bar(self,master, widget,is_vbar,is_hbar, x, y, w, h, pw, ph):
        vbar, hbar = None, None
        if is_vbar:
            vbar = Scrollbar(master)
            self.v_scrollbar(vbar, widget, x, y, w, h, pw, ph)
        if is_hbar:
            hbar = Scrollbar(master, orient="horizontal")
            self.h_scrollbar(hbar, widget, x, y, w, h, pw, ph)
        self.scrollbar_autohide(vbar, hbar, widget)
    def __tk_label_lwlmbyq6(self,parent):
        label = Label(parent,text="Song",anchor="center", )
        label.place(x=28, y=28, width=50, height=30)
        return label
    def __tk_input_lwlmc3ut(self,parent):
        ipt = Entry(parent, )
        ipt.place(x=80, y=28, width=452, height=30)
        return ipt
    def __tk_button_lwlmc5xy(self,parent):
        # btn = Button(parent, text="Open", takefocus=False,)
        # btn.place(x=530, y=28, width=50, height=30)
        return
    def __tk_button_lwlml4xr(self,parent):
        btn = Button(parent, text="Record", takefocus=False,command=lambda: btn_record(self.tk_input_lwlmc3ut,self.tk_label_lwlmqt4u))
        btn.place(x=116, y=81, width=50, height=30)
        return btn
    def __tk_button_lwlmlf6t(self,parent):
        btn = Button(parent, text="Live", takefocus=False,command=lambda: btn_live(self.tk_input_lwlmm4al,self.tk_label_lwlmqt4u))
        btn.place(x=180, y=80, width=50, height=30)
    def __tk_input_lwlmm4al(self,parent):
        ipt = Entry(parent, )
        ipt.place(x=241, y=80, width=343, height=30)
        return ipt
    def __tk_input_lwlmnfdy(self,parent):
        ipt = Entry(parent, )
        ipt.place(x=25, y=132, width=150, height=30)
        return ipt
    def __tk_button_lwlmnili(self,parent):
        btn = Button(parent, text="Advance SET", takefocus=False,command=lambda: btn_live_advance(self.tk_input_lwlmnfdy))
        btn.place(x=178, y=132, width=95, height=30)
        return btn
    def __tk_input_lwlmq24f(self,parent):
        ipt = Entry(parent, )
        ipt.place(x=26, y=178, width=150, height=30)
        return ipt
    def __tk_button_lwlmq4om(self,parent):
        btn = Button(parent, text="Delay SET", takefocus=False,command=lambda: btn_live_delay(self.tk_input_lwlmq24f))
        btn.place(x=178, y=178, width=96, height=30)
        return btn

    def __tk_label_lwlmqt4u(self, parent):
        label = Label(parent, text="00:00:00", anchor="center", )
        label.place(x=397, y=135, width=187, height=74)
        return label
    def __tk_label_lwlmslf1(self,parent):
        label = Label(parent,text="1. Arena请开启osc输入和输出\n2. 如果出现声画不同步请及时调整Delay值或者Advance值\n3. Live模式默认会有声音发出，请调整声卡或者音量以免导致现场音频冲突",anchor="center", )
        label.place(x=24, y=239, width=562, height=74)
        return label

    def __tk_button_lwlpelpp(self, parent):
        # btn = Button(parent, text="STOP", takefocus=False,command=btn_stop )
        # btn.place(x=293, y=132, width=88, height=75)
        return

    def __tk_button_lwlp9o5j(self, parent):
        # btn = Button(parent, text="stopsound", takefocus=False, command=btn_stopsound)
        # btn.place(x=34, y=81, width=71, height=30)
        return
class Win(WinGUI):
    def __init__(self, controller):
        self.ctl = controller
        super().__init__()
        self.__event_bind()
        self.__style_config()
        self.ctl.init(self)
    def __event_bind(self):
        pass
    def __style_config(self):
        pass
if __name__ == "__main__":
    win = WinGUI()
    win.mainloop()